package com.ny.view;

import com.ny.dao.StudentDao;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.Impl.ProductServiceImpl;
import com.ny.service.Impl.StudentServiceImpl;
import com.ny.service.OrderService;
import com.ny.service.ProductService;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class IndexView {
    private final ProductService productService = new ProductServiceImpl();

    public IndexView() throws SQLException, ClassNotFoundException {

    }

    public void indexView(Student student) throws Exception {
        System.out.println("请选择相应操作");
        System.out.println("1.个人信息界面");
        System.out.println("2.查看商品信息");
        System.out.println("3.查看订单信息");
        System.out.println("4.修改密码");
        System.out.println("5.修改电话号码");
        Scanner scanner = new Scanner(System.in);

        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                System.out.println("个人信息界面");
                System.out.println("学生ID: " + student.getStudent_Id());
                System.out.println("姓名: " + student.getStudentName());
                System.out.println("电话号码: " + student.getStudentPhone());
                break;
            case 2:
                System.out.println("商品信息界面");
                List<Product> proList = productService.findAllProduct();
                System.out.println("商品序号" + "\t" + "商品名称" + "\t" + "商品价格" + "\t" + "商品库存" + "\t" + "商品类别" + "\t" + "商品描述");
                if (proList != null) {
                    for (Product product : proList) {
                        System.out.print(product.getProduct_Id());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getName());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getPrice());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getStock());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getCategory());
                        System.out.print("\t" + "\t");
                        System.out.println(product.getDescription());
                    }
                }
                System.out.println("请输入你需要购买的商品序号");
                int productId = scanner.nextInt();
                System.out.println("请输入你需要购买的商品数量");
                int productNum = scanner.nextInt();

                int sid = student.getStudent_Id();
                double totalPrice = productService.findAllProduct().get(productId - 1).getPrice() * productNum;
                Order order = new Order(productId);
                order.setStudent_Id(sid);
                order.setProduct_Id(productId);
                order.setNumber(productNum);
                order.setTotal_Price(totalPrice);

                int i = OrderService.addOrder(order);
                if (i > 0) {
                    System.out.println("添加成功");
                } else {
                    System.out.println("添加失败");
                    indexView(student);
                }
                // 把商品的全部信息查询出来
                break;
            case 3:
                System.out.println("订单信息界面");
                System.out.println("订单编号" + "\t" + "用户编号" + "\t" + "购买数量" + "\t" + "购买价格" + "\t" + "购买时间");

                List<Order> orderList = OrderService.findAllOrder();
                assert orderList != null;
                for (Order o : orderList) {
                    System.out.println(o.getOrder_Id() + "\t" + o.getStudent_Id() + "\t" + o.getNumber() + "\t" + o.getTotal_Price() + "\t");
                }
                break;

        }
    }
}
